﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Kawanon.Usability;

namespace Kawanon.QtHoney
{
	public partial class GestureEditForm : Form
	{
		public GestureExpression Expression { get; set; }

		private bool m_updating = false;

		public GestureEditForm()
		{
			InitializeComponent();
		}

		private void UpdateExpression(GestureExpression expr)
		{
			Expression = expr;

			m_updating = true;
			textBoxUrdl.Text = expr.FormatTrails();
			textBoxUrdl.SelectionStart = textBoxUrdl.TextLength;
			gesturePanel1.Focus();
			m_updating = false;
		}

		private void gesturePanel1_GestureDetected(object sender, GestureEventArgs e)
		{
			UpdateExpression(e.Expression);
		}

		private void gesturePanel1_MaybeGestureStarted(object sender, MouseEventArgs e)
		{
			//textBoxUrdl.Clear();
		}

		private void gesturePanel1_TrailDetected(object sender, GestureEventArgs e)
		{
			UpdateExpression(e.Expression);
		}

		private void GestureEditForm_Load(object sender, EventArgs e)
		{
			if (Expression != null)
			{
				UpdateExpression(Expression);

				textBoxName.Text = Expression.Name;
			}
		}

		private void textBoxUrdl_TextChanged(object sender, EventArgs e)
		{
			if (!m_updating && Expression != null)
			{
				m_updating = true;
				try
				{
					Expression = new GestureExpression(textBoxUrdl.Text, Expression.Action);
				}
				catch (FormatException)
				{
					System.Media.SystemSounds.Beep.Play();
				}
				m_updating = false;
				textBoxUrdl.Focus();
			}
		}
	}
}
